/**
 * Created by Aigo on 2016/4/19 0019.
 */

/**
 * 根据公司id，获取公司名称
 * @param id
 * @returns {*}
 */
var getip="http://192.168.1.69:3000/";
function getCompany(id){
    var name;
    if(id == ""){
        name="未设置";
    }else{
        $.ajax({
            url:getip+"company/getCompany?id="+id,
            async: false,
            success : function(data){
                name=data.rows[0].company_name;
            }
        });
    }

    return name;
};

/**
 * 根据部门id获取部门基本信息
 * @param id
 * @returns {*}
 */
function getDeparment(id){
    var name;
    if(id == null || id == ""){
        name="未设置";
    }else{
        $.ajax({
            url:getip+"department/getDepartment?id="+id,
            async: false,
            success : function(data){
                name=data.rows[0].department_name;
            }
        });
    }

    return name;
}

/**
 * 根据人员Id，获取人员基本信息
 * @param id
 * @returns {*}
 */
function getEmpInfo(userId){
    var emphtml=[];
    $.ajax({
        url:getip+"employee/getEmployees?userId="+userId,
        async: false,
        success : function(data){
            emphtml=data.rows;
        }
    });
    return emphtml;
}


/**
 * 根据人员Id，获取阶段人员基本信息
 * @param id
 * @returns {*}
 */
function getProEmpInfo(userId,proEmpId,projectId){
    var emphtml="";
    $.ajax({
        url:getip+"employee/list?userId="+userId,
        async: false,
        success : function(data){
            var obj=data.rows[0];
            emphtml="<div nameId='"+proEmpId+"' class='feed-element'><a class='pull-left'>" +
                "<img alt='image' class='img-circle' src='"+obj.headImg+"'></a>" +
                "<div class='media-body '><small class='pull-right'>" +
                "<span class='label label-danger' value='"+proEmpId+"'onClick='delProEmp(\""+proEmpId+"\",\""+userId+"\",\""+projectId+"\")'>删除</span></small>" +
                "<strong>"+obj.name+"</strong></div></div>";

        }
    });
    return emphtml;
}




/**
 * 删除参与人员
 * @param proEmpId
 */
function delProEmp(proEmpId){
    $.ajax({
        url:getip+"empProject/deleteEmpProject?id="+proEmpId,
        async: false,
        success : function(data){
            if(data.success){
                $("div[nameId="+proEmpId+"]").remove();
            }
        }
    });
}



/**
 * 获取全部公司
 */
function getCompanyAll(){
    var companyHtml="";
    $.ajax({
        url:getip+"company/getCompanys",
        async: false,
        success : function(data){
            if(data.success){
                 companyHtml="<option value='0'>请选择公司</option>";
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    companyHtml+="<option value='"+obj.id+"'>"+obj.company_name+"</option>";
                }
            }
        }
    });
    return companyHtml;
}

/**
 * 根据公司获取部门
 * @param comId
 * @returns {string}
 */
function getDepAll(companyId){
    var departmentHtml="";
    $.ajax({
        url:getip+"department/getDepartments?companyId="+companyId,
        async: false,
        success : function(data){
            if(data.success){
                   departmentHtml="<option value='0'>请选择部门</option>";
                    for(var i=0;i<data.rows.length;i++){
                        var obj=data.rows[i];
                        departmentHtml+="<option value='"+obj.id+"'>"+obj.department_name+"</option>";
                    }

                }
        }
    });
    return departmentHtml;
}

/**
 * 根据部门选择人员
 */
function getEmp(department){
    var employeeHtml="";
    $.ajax({
        url:getip+"employee/list?department="+department,
        async: false,
        success : function(data){
            if(data.success){
                employeeHtml="<option value='0'>请选择成员</option>";
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    employeeHtml+="<option dep='"+department+"' value='"+obj.userId+"'>"+obj.name+"</option>";
                }
            }
        }
    });
    return employeeHtml;

}





/**
 * 根据公司获取项目
 */
function getProjectByComId(companyId){
    var projectHtml="";
    projectHtml="<option value='0'>请选择项目</option>";
    $.ajax({
        url:getip+"project/getProjectEmpHostUserId?companyId="+companyId,
        async: false,
        success : function(data){
            if(data.success){
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    projectHtml+="<option hostUserId='"+obj.user_id+"'  value='"+obj.id+"'>"+obj.project_short_name+"</option>";
                }

            }
        }
    });
    return projectHtml;
}

/**
 * 获取12个阶段
 */
function getStage(){
    var stages=[];
    $.ajax({
        url:getip+"stage/list",
        async: false,
        success : function(data){
            if(data.success){
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    var arr=[];
                    arr.push(obj._id);
                    arr.push(obj.stageName);
                    stages.push("["+arr+"]");
                }

            }
        }
    });
    return stages;
}

/**
 * 获取5个阶段
 */
function getProgress(){
    var stages=[];
    $.ajax({
        url:getip+"progress/list",
        async: false,
        success : function(data){
            if(data.success){
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    var arr=[];
                    arr.push(obj._id);
                    arr.push(obj.progressName);
                   // stages.push("["+arr+"]");
                    stages.push(arr);
                }

            }
        }
    });
    return stages;
}

/**
 * 根据项目获取阶段
 */
function getProjectStage(projectId){
    var stage=[];
    $.ajax({
        url:getip+"projectStage/getProjectStages?projectId="+projectId+"&progressId=0",
        async: false,
        success : function(data){
            if(data.success){
                for(var i=0;i<data.rows.length;i++){
                    var obj=data.rows[i];
                    var arr=[];
                    arr.push(obj.id);
                    arr.push(obj.stage_id);
                    //arr.push(obj.hostDepartmentId);
                   //arr.push(obj.hostUserId);
                    arr.push(obj.state);
                   // arr.push(obj._id);
                    arr.push(obj.priority);
                    arr.push(obj.project_id);
                    stage.push(arr);
                }

            }
        }
    });
    return stage;
}

var state_state=["已结束","未开始","更进中","暂停"];

/**
 * 阶段状态
 */
function stageState(){
    var html="<option value='4'>请选择状态</option>";
    for(var i=1;i<state_state.length;i++){
        html+="<option value='"+i+"'>"+state_state[i]+"</option>";
    }
    html+="<option value='0'>已结束</option>"
    return html;
}



var project_level=["优先级5","优先级4","优先级3","优先级2","优先级1"];
/**
 * 阶段优先级
 */
function stageLevel(){
    var html="<option value=''>请选择优先级</option>";
    for(var i=0;i<project_level.length;i++){
        html+="<option value='"+parseInt(i+1)+"'>"+project_level[i]+"</option>";
    }
    return html;
}
/**
 * 根据项目Id获取项目名称
 * @param projectId
 * @returns {string}
 */
function getProjectInfo(projectId){
    var projectName="";
    $.ajax({
        url:getip+"project/getProject?id="+projectId,
        async: false,
        success : function(data){
            if(data.success){
                var obj=data.rows[0];
                projectName=obj.project_short_name;
            }
        }
    });
    return projectName;
}

/**
 * 查询人员是否已经关联项目
 */
function getEmpPro(userId,projectId){
    var empProLen;
    $.ajax({
        url:getip+"empProject/getEmpProjects?userId="+userId+"&projectId="+projectId,
        async: false,
        success : function(data){
            if(data.success){
                if(data.rows.length == 0){
                    empProLen=data.rows.length;
                }else{
                    empProLen=data.rows[0]._id;
                }

            }
        }
    });
    return empProLen;
}

/**
 * 添加人员项目
 */
function addEmpPro(userId,projectId,userName,projectName){
    var succ;
    $.ajax({
        url:getip+"empProject/add?userId="+userId+"&projectId="+projectId+"&userName="+userName+"&ProjectName="+projectName,
        async: false,
        success : function(data){
            succ=data.success;
        }
    });
    return succ;
}

/**
 * 删除人员项目
 */
function delEmpPro(empProId){
    var succ;
    $.ajax({
        url:getip+"empProject/remove?_id="+empProId,
        async: false,
        success : function(data){
            succ=data.success;
        }
    });
    return succ;
}

/**
 * 根据人员id、项目id查询人员项目阶段表
 */
function getEmpProStage(userId,projectId){
    var EmpProStageLen;
    $.ajax({
        url:getip+"stageEmployee/list?UserId="+userId+"&projectId="+projectId,
        async: false,
        success : function(data){
            EmpProStageLen=data.rows.length;
        }
    });
    return EmpProStageLen;
}



/**
 * 根据项目阶段id获取阶段
 */
function getProjectStageInfo(projectStageId){
    var stage;
    $.ajax({
        url:getip+"projectStage/list?projectStageId="+projectStageId,
        async: false,
        success : function(data){
            if(data.success){
                stage=data.rows;
            }
        }
    });
    return stage;
}



